package com.bestillet.defaultmultishop;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CategoryArrayAdapter extends ArrayAdapter<Category> {
	private ArrayList<Category> categories;
	
	private Activity context;
	
	private String hexColor;

	public CategoryArrayAdapter(Context context, ArrayList<Category> categories, String hexColor) {
		super(context, R.layout.item_category, categories);
		this.categories = categories;
		this.context = (Activity) context;
		this.hexColor = hexColor;
	}

	static class ViewHolder {
		protected TextView text;
		protected TextView extra;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Category c = categories.get(position);
		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.item_category, parent, false);
			final ViewHolder viewHolder = new ViewHolder();
				
			viewHolder.text = (TextView) view.findViewById(R.id.lblCategory);
			viewHolder.extra = (TextView) view.findViewById(R.id.lblExtra);
			view.setTag(viewHolder);
		} else {
			view = convertView;
		}
		
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.text.setText(c.getTitle().substring(0, 1).toUpperCase(Locale.ENGLISH) + c.getTitle().substring(1).toLowerCase(Locale.ENGLISH));
		holder.text.setTextColor(Color.parseColor(hexColor));
		holder.extra.setText(Html.fromHtml(c.getExtra().replace("\\n", "<br>")));
		holder.extra.setVisibility(c.getExtra().equals("") ? View.GONE : View.VISIBLE);
		return view;
	}
}